package com.ibt.controller;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.ibt.util.SendMail;

public class MailController {
	@RequestMapping( "/sendMail.do") 
	public ModelAndView sendMail(HttpServletRequest request,HttpSession session, ModelAndView model) throws Exception{
		String from="raja.mca.du2009@gmail.com";
		System.out.println("User Email Id Is:"+from);
		String pass="rajabugtrack";
		System.out.println("User password Is:"+pass);
		System.out.println("Email is:"+from+" Password is:"+pass);
	
	
				SendMail sm=new SendMail();
				String sourceName=request.getParameter("sourceName").trim();
				String refno=request.getParameter("refno").trim();
			
				String soEmail="rajakmca09@gmail.com";
				String sub=request.getParameter("subject");
				String srowCount=request.getParameter("rowCount");
				String enqnum=request.getParameter("enqnum");
				
				
				StringBuilder sb=new StringBuilder();
				sb.append("Enquiry Number Is "+enqnum+"\n");
				sb.append("<h2>Hi</h2><h2>"+sub+"</h2> <br>");
				sb.append("<table border=1 width='890'><tr><th width='30'>S.No</th> <th width='200'> Part</th><th width='50'> Quantity</th> <th width='200'> Make</th> <th width='200'> Description </th><th width='200'> Application </th></tr>" );
				int rowCount=Integer.parseInt(srowCount);
				for(int i=0;i<rowCount;i++){
					//logger.info(request.getParameter("part"+(i+1)));
					String part=request.getParameter("part"+(i+1));
					String qty=request.getParameter("quantity"+(i+1));
					String make=request.getParameter("make"+(i+1));
					String desc=request.getParameter("description"+(i+1));
					String app=request.getParameter("application"+(i+1));
					sb.append("<tr><td align='center'>"+(i+1)+"</td><td>"+part+"</td><td align='center'>"+qty+"</td><td>"+make+"</td><td>"+desc+"</td><td>"+app+"</td></tr>");
					
				}
				sb.append("</table>");
				sb.append("<br><h3>Regards</h3><h2>Santosh</h2>");
				
				
				File file = null;
				byte[] logo=null;
				MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
				MultipartFile fileUpload = multipartRequest.getFile("attachment");
				String name = "Mail Has Sent Successfully ";
				String message=null;
				if(fileUpload!=null){
					logo = fileUpload.getBytes();
					name = fileUpload.getOriginalFilename();
					
					System.out.println("The Logo Name Is:"+name);
					System.out.println("The Logo  Is:"+logo);
					try{
						sm.sendMail(from,pass,sb.toString(), sub, soEmail, file, name);
						try{
							//psdao.updateMailSent(op.getId(), s.getId(), busid);
						}catch(Exception e){
							e.printStackTrace();
							message="Mail Has Sent.DB Updating Failed";
						}
					}catch(Exception e){
						e.printStackTrace();
						message="Mail Sending Failed. Please Try Again";
					}
					
					
				}
			else{
				try{
					sm.sendMailNoFile(from,pass,sb.toString(), sub, soEmail);
					try{
						//psdao.updateMailSent(op.getId(), s.getId(), busid);
					}catch(Exception e){
						e.printStackTrace();
						message="Mail Has Sent.DB Updating Failed";
					}
				}catch(Exception e){
					e.printStackTrace();
					message="Mail Sending Failed. Please Try Again";
				}
				
				
			}
				
				model = new ModelAndView("/jsp/sendMailPopUp.jsp");
				model.addObject("reflist",refno);
				model.addObject("msg",message);
				return model;
	}

}
